/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
#include <vector>
using namespace std;
class Solution {
    vector<TreeNode *> now, last;
    vector<vector<int> > ans;
    vector<int> tmp;
public:
    vector<vector<int> > levelOrderBottom(TreeNode *root) {
        now.clear();
        if (root!=NULL) now.push_back(root);
        while (!now.empty()) {
            last.swap(now);
            now.clear();
            tmp.clear();
            for (int i=0;i<last.size();i++){
                tmp.push_back(last[i]->val);
                if (last[i]->left!=NULL) now.push_back(last[i]->left);
                if (last[i]->right!=NULL) now.push_back(last[i]->right);
            }
            ans.push_back(tmp);
        }
        for (int i=0;i<ans.size()/2;i++)
            ans[i].swap(ans[ans.size()-1-i]);
        return ans;
    }
};
